home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Installer SDK 1.2 / Upgrader 1.2.1 & Engines / Upgrader 1.2.1 / Plug-in Examples / Common Files / Editor Utilities / CFileRefEditorWindow.cp next >
Encoding:
Text File  |  1998-08-25  |  12.1 KB  |  466 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. #include "EditorUtilities.h"
  4.  
  5. #include "CFileRefEditorWindow.h"
  6.  
  7. const ResType     kSelectFileB                     = 'slfB';
  8. const ResType     kRemoveFileRefB                 = 'rmvB';
  9. const ResType     kFileNameET                     = 'fnmE';
  10. const ResType     kFileTypeET                     = 'ftpE';
  11. const ResType     kFileCreatorET                     = 'fcrE';
  12. //const ResType     kCreationDateET                 = 'fcdE';
  13. const ResType     kMediaTypePM                     = 'mptP';
  14. const ResType     kFilePathET                     = 'fphE';
  15.  
  16. #define    kUnknownMedia        0        // Net install setup on CD-ROM or hard disk.  Use relative path from Upgrader application.
  17. #define    kFloppyDiskMedia    1        // Multiple floppy disk set.  Use full path starting with floppy volume name.
  18. #define kCDROMDiskMedia     2        // Multiple CD-ROM disk set.  Use full path starting with CD-ROM volume name.
  19.  
  20.  
  21. SInt32 DoEditFileRef( SInt16 inFileRefNum, SInt16 &inFileRefRsrcID, Boolean inSharedFileRef )
  22. {
  23.     // Return values:  0 = Changes accepted, 1 = Canceled, no changes made,  < 0 = Internal error, no changes mad
  24.     SInt32    result = -1;
  25.     
  26.     SInt16    savedResFile = CurResFile();
  27.     UseResFile( inFileRefNum );
  28.     
  29.     try
  30.     {
  31.         CFileRefEditorWindow*    theEditorWindow = new CFileRefEditorWindow( inFileRefNum, inFileRefRsrcID, inSharedFileRef );
  32.  
  33.         if( inFileRefRsrcID == 0 )
  34.             theEditorWindow->mFileRefRsrcID = GetUniqueIDForResType( inFileRefNum, 'flrf' );
  35.  
  36.         result = theEditorWindow->DoEdit();
  37.         
  38.         delete theEditorWindow;
  39.         
  40.         if( result == 0 || result == 2  )
  41.             inFileRefRsrcID = theEditorWindow->mFileRefRsrcID;
  42.             
  43.     }
  44.     catch( OSErr theErr )
  45.     {
  46.         ;
  47.     }
  48.     
  49.     UseResFile( savedResFile );
  50.     
  51.     return result;
  52.  
  53. }
  54.  
  55. void GetPathFromFileRef( SInt16 inFileRefNum, SInt16 inFileRefRsrcID, Str255 outFilePathForDisplay )
  56. {
  57.     
  58.     SInt16    savedResFile = CurResFile();
  59.     UseResFile( inFileRefNum );
  60.  
  61.     try
  62.     {
  63.         outFilePathForDisplay[0] = 0;
  64.         
  65.         Handle         theRsrcHandle = Get1Resource( 'flrf', inFileRefRsrcID );
  66.         
  67.         if( theRsrcHandle )
  68.         {
  69.             TRsrcHandle* theRsrcStream = new TRsrcHandle( theRsrcHandle );
  70.             SignalIf_(theRsrcStream == nil);
  71.             
  72.             if( theRsrcStream->ReadShort() == 0)
  73.             {
  74.                 // No flags at the moment
  75.                 theRsrcStream->ReadShort();    
  76.                 
  77.                 // Set Media Type Popup
  78.                 theRsrcStream->ReadShort();
  79.                 
  80.                 // Set Type
  81.                 theRsrcStream->ReadLong();
  82.                 
  83.                 // Set Creator
  84.                 theRsrcStream->ReadLong();
  85.                 
  86.                 // Set Creation Date
  87.                 theRsrcStream->ReadLong();
  88.                 
  89.                 // Set File Path name
  90.                 theRsrcStream->ReadPStrIntoStr255( outFilePathForDisplay );
  91.                 
  92.                 // Set File name
  93.                 Str255    theFileName;
  94.                 theRsrcStream->ReadPStrIntoStr255( theFileName );
  95.                 PStrConcat( outFilePathForDisplay, theFileName );
  96.  
  97.             }
  98.             
  99.             delete theRsrcStream;
  100.  
  101.         }
  102.         
  103.  
  104.     }
  105.     catch( OSErr theErr )
  106.     {
  107.         ;
  108.     }
  109.  
  110.     UseResFile( savedResFile );
  111. }
  112.  
  113. // ---------------------------------------------------------------------------
  114. //        • CFileRefEditorWindow
  115. // ---------------------------------------------------------------------------
  116. CFileRefEditorWindow::CFileRefEditorWindow( SInt16 inFileRefNum, SInt16 inFileRefRsrcID, Boolean inSharedFileRef )
  117.     : StDialogHandler(133, NULL), mFileRefNum(inFileRefNum), mFileRefRsrcID(inFileRefRsrcID), mSharedFileRef(inSharedFileRef)
  118. {
  119. }
  120.  
  121. // ---------------------------------------------------------------------------
  122. //        • ~CFileRefEditorWindow
  123. // ---------------------------------------------------------------------------
  124. CFileRefEditorWindow::~CFileRefEditorWindow()
  125. {
  126. }
  127.  
  128.  
  129. SInt32    CFileRefEditorWindow::DoEdit()
  130. {
  131.     
  132.     LWindow            *theDialog = GetDialog();
  133.  
  134.     this->LoadWindowFromFile();
  135.     
  136.     theDialog->Show();
  137.     
  138.     LEditField*    theField = (LEditField*) mDialog->FindPaneByID(kFileNameET);
  139.     SignalIf_(theField == nil);
  140.     theDialog->SetLatentSub(theField);
  141.  
  142.     SInt32        result = -1;
  143.     
  144.     while (true) {
  145.         MessageT    hitMessage = DoDialog();
  146.         
  147.         if (hitMessage == msg_Cancel) {
  148.             result = 1;
  149.             break;
  150.             
  151.         } else if (hitMessage == kRemoveFileRefB) {
  152.         
  153.             if( mSharedFileRef ) {
  154.                 mFileRefRsrcID = 0;
  155.                 result = 0;
  156.             }
  157.             else {
  158.                 Handle theRsrcHandle = Get1Resource( 'flrf', mFileRefRsrcID );
  159.                 if( theRsrcHandle ) {
  160.                     RemoveResource( theRsrcHandle );
  161.                     DisposeHandle( theRsrcHandle );
  162.                 }
  163.                 mFileRefRsrcID = 0;
  164.                 result = 2;
  165.             }
  166.             break;
  167.             
  168.         } else if (hitMessage == kSelectFileB) {
  169.             
  170.             SFTypeList typeList;
  171.             StandardFileReply    reply;
  172.             StandardGetFile( NULL, -1, typeList, &reply );    
  173.             
  174.             if( reply.sfGood )
  175.             {
  176.             
  177.                 // Set Media Type Popup
  178.                 LStdPopupMenu* thePopupMenu = (LStdPopupMenu*) mDialog->FindPaneByID(kMediaTypePM);
  179.                 SignalIf_(thePopupMenu == nil);
  180.             
  181.             
  182.                 Str255             thePathStr;
  183.                 ResType         theFileType;
  184.                 ResType         theFileCreator;
  185.                 UInt32             theFileCreationDate;
  186.                 GetInfoFromFSSpec( &reply.sfFile, mFileRefNum, thePopupMenu->GetValue() > 1, thePathStr, theFileType, theFileCreator, theFileCreationDate );
  187.                 
  188.                 // Set Type
  189.                 LEditField* theField = (LEditField*) mDialog->FindPaneByID(kFileTypeET);
  190.                 SignalIf_(theField == nil);
  191.                 LStr255 aType(theFileType);
  192.                 theField->SetDescriptor( StringPtr(aType) );
  193.                 
  194.                 // Set Creator
  195.                 theField = (LEditField*) mDialog->FindPaneByID(kFileCreatorET);
  196.                 SignalIf_(theField == nil);
  197.                 LStr255 anotherType(theFileCreator);
  198.                 theField->SetDescriptor( StringPtr(anotherType) );
  199. /*                
  200.                 // Set Creation Date
  201.                 theField = (LEditField*) mDialog->FindPaneByID(kCreationDateET);
  202.                 SignalIf_(theField == nil);
  203.                 Str255    theDateStr;
  204.                 DateString( theFileCreationDate, longDate, theDateStr, nil);
  205.                 theField->SetDescriptor( theDateStr );
  206. */                
  207.                 // Set File Path name
  208.                 theField = (LEditField*) mDialog->FindPaneByID(kFilePathET);
  209.                 SignalIf_(theField == nil);
  210.                 theField->SetDescriptor( thePathStr );
  211.                 
  212.                 // Set File name
  213.                 theField = (LEditField*) mDialog->FindPaneByID(kFileNameET);
  214.                 SignalIf_(theField == nil);
  215.                 theField->SetDescriptor( reply.sfFile.name );
  216.             }
  217.         
  218.         } else if (hitMessage == msg_OK) {
  219.             this->SaveWindowToFile();
  220.             result = 0;
  221.             break;
  222.         }
  223.     }
  224.     
  225.     return result;
  226. }
  227.  
  228.  
  229. void CFileRefEditorWindow::SaveWindowToFile()
  230. {
  231.     
  232.     TRsrcHandle* theRsrcStream = new TRsrcHandle();
  233.     SignalIf_(theRsrcStream == nil);
  234.     
  235.     theRsrcStream->AppendShort( 0 );
  236.     theRsrcStream->AppendShort( 0 );
  237.             
  238.     // Set Media Type Popup
  239.     LStdPopupMenu* thePopupMenu = (LStdPopupMenu*) mDialog->FindPaneByID(kMediaTypePM);
  240.     SignalIf_(thePopupMenu == nil);
  241.     theRsrcStream->AppendShort( thePopupMenu->GetValue() - 1 );
  242.     
  243.     // Set Type
  244.     LEditField* theField = (LEditField*) mDialog->FindPaneByID(kFileTypeET);
  245.     SignalIf_(theField == nil);
  246.     LStr255    theTypeStr;
  247.     theField->GetDescriptor( theTypeStr );
  248.     if( theTypeStr[0] == 0 )
  249.         theRsrcStream->AppendLong( 0 );
  250.     else
  251.         theRsrcStream->AppendLong( FourCharCode( theTypeStr ) );
  252.     
  253.     // Set Creator
  254.     theField = (LEditField*) mDialog->FindPaneByID(kFileCreatorET);
  255.     SignalIf_(theField == nil);
  256.     LStr255    theCrtrStr;
  257.     theField->GetDescriptor( theCrtrStr );
  258.     if( theCrtrStr[0] == 0 )
  259.         theRsrcStream->AppendLong( 0 );
  260.     else
  261.         theRsrcStream->AppendLong( FourCharCode( theCrtrStr ) );
  262.  
  263. /*    
  264.     // Set Creation Date
  265.     theField = (LEditField*) mDialog->FindPaneByID(kCreationDateET);
  266.     SignalIf_(theField == nil);
  267.     Str255    theDateStr;
  268.     theField->GetDescriptor( theDateStr );
  269.     DateCacheRecord    theCache;
  270.     long        lengthUsed;
  271.     LongDateRec    dateTime;
  272.     InitDateCache( &theCache );
  273.     StringToDate( (char*)theDateStr, theDateStr[0], &theCache, &lengthUsed, &dateTime);
  274.     StringToTime( (char*)theDateStr, theDateStr[0], &theCache, &lengthUsed, &dateTime);
  275.     unsigned long        dateInSecs;
  276.     DateToSeconds( &(dateTime.od.oldDate), &dateInSecs );
  277.     theRsrcStream->AppendLong( dateInSecs );
  278. */
  279.     theRsrcStream->AppendLong( 0 );    // Always set Creation Date to 0 for now.
  280.     
  281.     // Set File Path name
  282.     theField = (LEditField*) mDialog->FindPaneByID(kFilePathET);
  283.     SignalIf_(theField == nil);
  284.     Str255    theFilePath;
  285.     theField->GetDescriptor( theFilePath );
  286.     if( theFilePath[0] == 0 ) {
  287.         theFilePath[0] = 1;
  288.         theFilePath[1] = ':';
  289.     }
  290.     theRsrcStream->AppendStr255( theFilePath );
  291.     
  292.     // Set File name
  293.     theField = (LEditField*) mDialog->FindPaneByID(kFileNameET);
  294.     SignalIf_(theField == nil);
  295.     Str255    theFileName;
  296.     theField->GetDescriptor( theFileName );
  297.     theRsrcStream->AppendStr255( theFileName );
  298.  
  299.     theRsrcStream->Write( mFileRefNum, 'flrf', mFileRefRsrcID );
  300.             
  301.     delete theRsrcStream;
  302. }
  303.  
  304.  
  305. void CFileRefEditorWindow::LoadWindowFromFile()
  306. {
  307.     
  308.     Handle         theRsrcHandle = Get1Resource( 'flrf', mFileRefRsrcID );
  309.     
  310.     if( theRsrcHandle )
  311.     {
  312.         TRsrcHandle* theRsrcStream = new TRsrcHandle( theRsrcHandle );
  313.         SignalIf_(theRsrcStream == nil);
  314.         
  315.         if( theRsrcStream->ReadShort() == 0)
  316.         {
  317.         
  318.             theRsrcStream->ReadShort();    // No flags at the moment
  319.             
  320.             // Set Media Type Popup
  321.             LStdPopupMenu* thePopupMenu = (LStdPopupMenu*) mDialog->FindPaneByID(kMediaTypePM);
  322.             SignalIf_(thePopupMenu == nil);
  323.             thePopupMenu->SetValue( theRsrcStream->ReadShort() + 1 );
  324.             
  325.             // Set Type
  326.             LEditField* theField = (LEditField*) mDialog->FindPaneByID(kFileTypeET);
  327.             SignalIf_(theField == nil);
  328.             LStr255 aType((ResType)theRsrcStream->ReadLong());
  329.             theField->SetDescriptor( StringPtr(aType) );
  330.             
  331.             // Set Creator
  332.             theField = (LEditField*) mDialog->FindPaneByID(kFileCreatorET);
  333.             SignalIf_(theField == nil);
  334.             LStr255 anotherType((ResType)theRsrcStream->ReadLong());
  335.             theField->SetDescriptor( StringPtr(anotherType) );
  336.  
  337.             theRsrcStream->ReadLong();        // Ignore Creation Date for now
  338. /*            
  339.             // Set Creation Date
  340.             theField = (LEditField*) mDialog->FindPaneByID(kCreationDateET);
  341.             SignalIf_(theField == nil);
  342.             Str255    theDateStr;
  343.             DateString( theRsrcStream->ReadLong(), longDate, theDateStr, nil);
  344.             theField->SetDescriptor( theDateStr );
  345. */            
  346.             // Set File Path name
  347.             theField = (LEditField*) mDialog->FindPaneByID(kFilePathET);
  348.             SignalIf_(theField == nil);
  349.             Str255    theFilePath;
  350.             theRsrcStream->ReadPStrIntoStr255( theFilePath );
  351.             theField->SetDescriptor( theFilePath );
  352.             
  353.             // Set File name
  354.             theField = (LEditField*) mDialog->FindPaneByID(kFileNameET);
  355.             SignalIf_(theField == nil);
  356.             Str255    theFileName;
  357.             theRsrcStream->ReadPStrIntoStr255( theFileName );
  358.             theField->SetDescriptor( theFileName );
  359.  
  360.         }
  361.         
  362.         delete theRsrcStream;
  363.  
  364.     }
  365. }
  366.  
  367.  
  368.  
  369. void CFileRefEditorWindow::GetInfoFromFSSpec( FSSpec* inFSSpec, SInt16 inDocRefNum, Boolean inFullPath, Str255 outPathStr, ResType &outFileType, ResType &outFileCreator, UInt32 &outFileCreationDate )
  370. {
  371.  
  372.  
  373.     CInfoPBRec            cInfo;
  374.     HFileInfo*            hfipb             = (HFileInfo *)&cInfo;
  375.  
  376.     // we need to get this file's CInfoPBRec so that we know
  377.     // its size, mod date, creation date...
  378.     hfipb->ioCompletion = nil;
  379.     hfipb->ioNamePtr = inFSSpec->name;
  380.     hfipb->ioVRefNum = inFSSpec->vRefNum;
  381.     hfipb->ioDirID = inFSSpec->parID;
  382.     hfipb->ioFDirIndex = 0;
  383.     
  384.     if( ::PBGetCatInfoSync(&cInfo) == noErr )
  385.     {
  386.         outFileType             = hfipb->ioFlFndrInfo.fdType;
  387.         outFileCreator             = hfipb->ioFlFndrInfo.fdCreator;
  388.         outFileCreationDate     = hfipb->ioFlCrDat;
  389.         BuildSrcPath( inFSSpec, inDocRefNum, inFullPath, outPathStr );
  390.     }
  391.     
  392. }
  393.  
  394.  
  395. // ---------------------------------------------------------------------------
  396. //        • BuildSrcPath
  397. // ---------------------------------------------------------------------------
  398.  
  399. void CFileRefEditorWindow::BuildSrcPath( FSSpec* inFSSpec, SInt16 inDocRefNum, Boolean inFullPath, Str255 outPathStr )
  400. {
  401.  
  402.     // Get the directory of the document, to use in determining a partial path
  403.     SInt32        partialPathDirID    = 0;
  404.     
  405.     if( ! inFullPath )
  406.     {
  407.         FCBPBRec    paramBlock;
  408.         Str255        fName;
  409.         
  410.         paramBlock.ioNamePtr     = fName;
  411.         paramBlock.ioFCBIndx     = 0;
  412.         paramBlock.ioRefNum     = inDocRefNum;
  413.  
  414.         if ( PBGetFCBInfoSync( ¶mBlock ) == noErr)
  415.             partialPathDirID = paramBlock.ioFCBParID;
  416.     }
  417.  
  418.     // Build the path string
  419.     CInfoPBRec            cInfo;
  420.     DirInfo*            dipb             = (DirInfo *)&cInfo;
  421.     char colon = ':';
  422.     Str255    theStr;
  423.     SInt32    theVol = inFSSpec->vRefNum;
  424.     SInt32    theDirID = inFSSpec->parID;
  425.     OSErr     aErr = noErr;
  426.     Boolean first = true;
  427.     
  428.     theStr[0] = 0;
  429.     
  430.     outPathStr[0] = 0;
  431.     
  432.     LStr255 buildStr;
  433.  
  434.     do
  435.     {
  436.         dipb->ioCompletion = nil;
  437.         dipb->ioNamePtr = theStr;
  438.         dipb->ioVRefNum = theVol;
  439.         dipb->ioDrDirID = theDirID;
  440.         dipb->ioFDirIndex = -1;
  441.         aErr = ::PBGetCatInfoSync(&cInfo);
  442.         if (aErr == noErr)
  443.         {
  444.             if( !inFullPath && theDirID == partialPathDirID )
  445.                 break;
  446.                 
  447.             buildStr = colon + buildStr; 
  448.  
  449.             buildStr = theStr + buildStr; 
  450.             theStr[0] = 0;
  451.             
  452.         }
  453.         theDirID = dipb->ioDrParID;
  454.         first = false;
  455.         
  456.     }
  457.     while (aErr == noErr);
  458.     
  459.     if( !inFullPath )
  460.         buildStr = colon + buildStr;
  461.  
  462.     LString::CopyPStr(StringPtr(buildStr), outPathStr, sizeof(Str255));
  463.  
  464. }
  465.  
  466.